import pika
import time
import json

credentials = pika.PlainCredentials("cluster-ms-cmall", "cluster-ms-cmall")
parameters = pika.ConnectionParameters(host="10.129.83.213", virtual_host='/backup', credentials=credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
queue = channel.queue_declare(queue='queue.phoenix.erp.order.stock.out', durable=True)
while True:  # 循环向队列中发送信息

    m = {"orderId": "DD20190712190005806", "outId": "CK201907120009", "type": 0, "goodsInfo": [
        {"orderId": "DD20190712190005806", "goodsId": "LS1562910958411903633", "name": "车顶面板 活动天窗", "quantity": 1}],
         "outTime": "2019-07-12 14:39:27"};

    message = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    print(json.dumps(m, ensure_ascii=False))
    channel.basic_publish(exchange='exchanges.phoenix.erp',
                          routing_key='routing.phoenix.erp.order.stock.out',
                          body=json.dumps(m, ensure_ascii=False))

    print('send message: %s' % message)

    while True:
        # 检查队列，以重新得到消息计数
        queue = channel.queue_declare(queue='queue.phoenix.erp.order.stock.out', passive=True)
        messageCount = queue.method.message_count
        print('messageCount: %d' % messageCount)
        if messageCount < 100:
            break
        time.sleep(1)

    # connection.close()

connection.close()
